package com.road.vast.system.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.road.vast.common.utils.PermissionConstants;
import com.road.vast.pojo.system.PePermission;
import com.road.vast.pojo.system.PeRole;
import com.road.vast.pojo.system.PeRolePermission;
import com.road.vast.system.component.IdWorkerComponent;
import com.road.vast.system.permission.mapper.PePermissionMapper;
import com.road.vast.system.permission.mapper.PeRolePermissionMapper;
import com.road.vast.system.role.mapper.PeRoleMapper;
import com.road.vast.system.role.service.PeRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-07-16
 */
@Service
public class PeRoleServiceImpl extends ServiceImpl<PeRoleMapper, PeRole> implements PeRoleService {
    @Resource
    PeRoleMapper peRoleMapper;

    @Autowired
    IdWorkerComponent idWorkerComponent;

    @Resource
    PePermissionMapper permissionMapper;

    @Resource
    PeRolePermissionMapper rolePermissionMapper;

    @Override
    public Integer saveRole(PeRole peRole) {
        String nextId = idWorkerComponent.getIdWorker().nextId() + "";
        peRole.setId(nextId);
        return peRoleMapper.insert(peRole);
    }

    @Override
    public Integer updateRole(PeRole peRole) {
        return peRoleMapper.updateById(peRole);
    }

    @Override
    public PeRole getById(String id) {
        return peRoleMapper.selectById(id);
    }

    @Override
    public Page<PeRole> getAll(Integer page, Integer pageSize) {
        Page<PeRole> rolePage = new Page<>(page, pageSize);
        return peRoleMapper.selectPage(rolePage, null);
    }

    @Override
    public List<PeRole> getList() {
        return peRoleMapper.selectList(null);
    }

    @Override
    public List<String> simple(String id) {
        QueryWrapper<PeRolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", id);
        List<PeRolePermission> rolePermissions = rolePermissionMapper.selectList(wrapper);
        List<String> permIds = new ArrayList<>();
        for (PeRolePermission rolePermission : rolePermissions) {
            permIds.add(rolePermission.getPermissionId());
        }
        return permIds;
    }

    @Override
    public Integer deleteRole(String id) {
        return peRoleMapper.deleteById(id);
    }

    @Override
    public Integer assignPrem(String roleId, List<String> permIds) {
        //删除该角色下的所有权限
        QueryWrapper<PeRolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        if(rolePermissionMapper.selectList(wrapper).size() > 0){
            int deleteRolePermission = rolePermissionMapper.delete(wrapper);
        }
        //给当前角色分配权限
        Integer result = 0;
        //添加菜单或按钮下的api权限id
        List<String> permIds01 = new ArrayList<>();
        for (String permId : permIds) {
            permIds01.add(permId);
        }
        for (String permId01 : permIds01) {
            QueryWrapper<PePermission> wrapper01 = new QueryWrapper<>();
            wrapper01.eq("type", PermissionConstants.PY_API)
                    .eq("parent_id", permId01);
            List<PePermission> permissions = permissionMapper.selectList(wrapper01);
            if (permissions.size() != 0){
                for (PePermission permission : permissions) {
                    System.out.println("**********" + permission.getId());
                    permIds.add(permission.getId());
                }
            }
        }
        //添加角色权限关系
        for (String permId : permIds) {
            String nextId = idWorkerComponent.getIdWorker().nextId() + "";
            PeRolePermission peRolePermission = new PeRolePermission(nextId, roleId, permId);
            result = rolePermissionMapper.insert(peRolePermission);
        }
        return result;
    }
}
